﻿using System.Data;
using Dapper;
using Md.Api.DDD;
using MySql.Data.MySqlClient;

namespace Md.Dapper.Repositories;
/// <summary>
/// 数据库陪配置
/// </summary>
public class DataBaseConfig
{

    /// <summary>
    /// 默认数据库连接字符串
    /// </summary>
    private static string DefaultSqlConnectionString = JsonConfig.JsonRead("MdSettings:MdConnection");
    /// <summary>
    /// 获取数据库连接
    /// </summary>
    /// <param name="sqlConnectionString">数据库连接字符串</param>
    /// <returns>IDbConnection</returns>
    public static IDbConnection GetSqlConnection(string? sqlConnectionString = null)
    {
        // 配置数据库中带下划线的表字段自动匹配无下划线的Model字段
        DefaultTypeMap.MatchNamesWithUnderscores = true;

        if (string.IsNullOrWhiteSpace(sqlConnectionString))
        {
            sqlConnectionString = DefaultSqlConnectionString;
        }

        IDbConnection conn = new MySqlConnection(sqlConnectionString);

        conn.Open();

        return conn;
    }
}

